package com.daoImpl;

import java.util.List;

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.dao.*;
import com.entity.Device;
import com.entity.UnitInterval;
import com.sun.org.apache.bcel.internal.generic.IF_ICMPGE;

public class UnitIntervalDaoImpl extends HibernateDaoSupport implements UnitIntervalDao {

	@Override
	public List<UnitInterval> findAllUnitInterval() {
		List list=(List<UnitInterval>)this.getHibernateTemplate().find("from UnitInterval");
        return list;
	}
	
	@Override
	public List<UnitInterval> findAuditingUnitInterval(){
		List list=(List<UnitInterval>)this.getHibernateTemplate().find("from UnitInterval");
        return list;
	}

	@Override
	public UnitInterval findUnitIntervalByID(int unitID) {
		return (UnitInterval) this.getHibernateTemplate().get(UnitInterval.class, unitID);
	}

	@Override
	public UnitInterval findUnitIntervalByUnitNoAndDeviceNo(String unitNo,String deviceNo) {
		List list = (List<UnitInterval>) this.getHibernateTemplate().find("from UnitInterval a where a.unitNo=? and a.deviceNo=?",new Object[]{unitNo,deviceNo});
		UnitInterval unitInterval = (UnitInterval)list.get(0);
		return unitInterval;
	}

	@Override
	public boolean addUnitInterval(UnitInterval unitInterval) {
		try{
            this.getHibernateTemplate().clear();
            this.getHibernateTemplate().save(unitInterval);
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
	}

	@Override
	public boolean deleteUnitInterval(UnitInterval unitInterval) {
		try{
            this.getHibernateTemplate().delete(unitInterval);
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
	}

	@Override
	public boolean updateUnitInterval(UnitInterval unitInterval) {
		try{
            this.getHibernateTemplate().clear();
            this.getHibernateTemplate().update(unitInterval);
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
	}

	@Override
	public List<UnitInterval> findUnitIntervalBySubstation(String substationNo) {
		List list = (List<UnitInterval>) this.getHibernateTemplate().find("from UnitInterval a where a.substationNo=? group by a.unitNo",new Object[]{substationNo});
		return list;
	}

	@Override
	public boolean checkUnitIntervalNoAvailable(String unitNo) {
		List list = (List<UnitInterval>) this.getHibernateTemplate().find("from UnitInterval a where a.unitNo=?",new Object[]{unitNo});
        if(list.get(0) != null)
        	return true;
        else return false;
	}

	@Override
	public void deleteUnitIntervalByUnitNo(String unitNo) {
		List list = (List<UnitInterval>) this.getHibernateTemplate().find("from UnitInterval a where a.unitNo=?",new Object[]{unitNo});
		if(list.size() != 0){
			for(int i=0;i<list.size();i++){
				UnitInterval unitInterval = (UnitInterval)list.get(i);
				deleteUnitInterval(unitInterval);
			}
		}
	}

	@Override
	public void deleteUnitIntervalBySubstationNo(String substationNo) {
		List list = (List<UnitInterval>) this.getHibernateTemplate().find("from UnitInterval a where a.substationNo=?",new Object[]{substationNo});
		if(list.size() != 0){
			for(int i=0;i<list.size();i++){
				UnitInterval unitInterval = (UnitInterval)list.get(i);
				deleteUnitInterval(unitInterval);
			}
		}
	}

}
